package com.shoujifeng.snowmusic.player;

import com.cnzz.mobile.android.sdk.MobileProbe;
import com.geniuseoe2012.lazyloaderdemo.cache.MusicPlayerImageLoader;
import com.shoujifeng.snowmusic.entity.AnimaAccess;
import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.player.http.DownloadManager;
import com.shoujifeng.snowmusic.player.http.GlobalValue;
import com.shoujifeng.snowmusic.player.http.ServerAccess;
import com.shoujifeng.snowmusic.player.utils.MediaUtil;
import com.shoujifeng.snowmusic.player.utils.MusicPlayer;
import com.shoujifeng.snowmusic.player.utils.TabHostAnim;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.Toast;

public class MyTabHost  extends TabActivity {
	private Button myMusicBtn;
	private Button onLineMusicBtn;
	private ImageButton musicImgBtn;
	private Intent mIntent1;
	private Intent mIntent2;
	private Intent mIntent3;
	private final int TAB_1		= 0;
	private final int TAB_2		= 1;
	private final int TAB_3		= 2;
	
	private TabHost mTabHost;
	private View mTmpView;
	public static ImageView playImageView;
	private AnimaAccess mAnimaAccess;
	
	private Animation operatingAnim;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_main);
		init();
		
		operatingAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);  
		LinearInterpolator lin = new LinearInterpolator();  
		operatingAnim.setInterpolator(lin);  
		mAnimaAccess = new AnimaAccess(this);
		
	GlobalValue.mParentContext = this;
	}

	@Override
	protected void onDestroy() {
		
		GlobalValue.g_DownloadManager.allStop();
		
		super.onDestroy();
	}
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onPause() {
		
		if(TabHostAnim.in!=0 && TabHostAnim.out!=0){
			super.overridePendingTransition(TabHostAnim.in, TabHostAnim.out);
			TabHostAnim.clear();
			}
		
		MobileProbe.onResume(this,"index"); 
		
		super.onPause();
	}

	@Override
	protected void onResume() {
		if(PlayService.mediaPlayer != null && PlayService.mediaPlayer.isPlaying()&&MusicPlayerActivity.isOnline==true){
			
			playImageView.setVisibility(View.GONE);
			MusicPlayerImageLoader xx = new MusicPlayerImageLoader(MyTabHost.this);
			xx.DisplayImage(MusicPlayerActivity.mOlineMusicList.get(MusicPlayer.position)
				.get(ServerAccess.ICON_URL).toString(),musicImgBtn, false,R.drawable.trans);
			musicImgBtn.startAnimation(operatingAnim);
		}
		else
		if(PlayService.mediaPlayer != null && PlayService.mediaPlayer.isPlaying()) {

			musicImgBtn.setBackgroundResource(R.drawable.icon_music_playing);

			//musicImgBtn.setBackgroundResource(R.drawable.icon_music_playing);
			musicImgBtn.getBackground().setAlpha(0);
			playImageView.setVisibility(View.VISIBLE);

			playImageView.setBackgroundResource(R.drawable.icon_music_playing_round);

			musicImgBtn.setImageResource(R.drawable.trans);

			Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon_music_playing); 
			musicImgBtn.setImageBitmap(MusicPlayerImageLoader.zoomImg(rawBitmap, 75, 75));

			musicImgBtn.startAnimation(operatingAnim);
			playImageView.setVisibility(View.VISIBLE);
		}  
		
		else {
			playImageView.setVisibility(View.VISIBLE);
			musicImgBtn.setBackgroundResource(R.drawable.icon_music_playing);
			musicImgBtn.setImageResource(R.drawable.trans);
			playImageView.setBackgroundResource(R.drawable.icon_music_play_round);
			
			musicImgBtn.clearAnimation();
		}
		 
		MobileProbe.onResume(this,"index"); 
		
		super.onResume();
	}

	private void init(){
		mTabHost = getTabHost();
		myMusicBtn     = (Button) findViewById(R.id.myMusicBtn);
		onLineMusicBtn = (Button) findViewById(R.id.onLineMusicBtn);
		musicImgBtn = (ImageButton) findViewById(R.id.musicPlayImgBtn);
		playImageView = (ImageView) findViewById(R.id.imgPlayScro);
		
		myMusicBtn.setOnClickListener(onClickListener);
		onLineMusicBtn.setOnClickListener(onClickListener);
		musicImgBtn.setOnClickListener(onClickListener);
		mTmpView = myMusicBtn;
		
		addContent();
		
	}
private View.OnClickListener onClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			if(mTmpView == v)
				return;
			
			int animTime = 300;
			switch (v.getId()) {
			
			case R.id.myMusicBtn:
			{
				
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, 1.0f, 0f, 0f, animTime, false));
				mTabHost.setCurrentTab(TAB_1);
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(-1.0f, 0f, 0f, 0f, animTime, false));
					onLineMusicBtn.setBackgroundResource(R.drawable.online);
					myMusicBtn.setBackgroundResource(R.drawable.mymusic);
					
					mTmpView = v;
			}
			break;
			case R.id.onLineMusicBtn:
			{
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, -1.0f, 0f, 0f, animTime, false));
				mTabHost.setCurrentTab(TAB_2);
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(1.0f, 0f, 0f, 0f, animTime, false));
				
				onLineMusicBtn.setBackgroundResource(R.drawable.btn_online_music_s);
				myMusicBtn.setBackgroundResource(R.drawable.btn_mymusic_normal);
				
				mTmpView = v;
			}
			break;
			case R.id.musicPlayImgBtn:
			{
				Intent intent = new Intent(MyTabHost.this,MusicPlayerActivity.class);
				if(MusicPlayer.musicInfos==null){
					MediaUtil mediaUtil = new MediaUtil();
					MusicPlayer.musicInfos = mediaUtil.getMp3Infos(MyTabHost.this);
					MusicPlayer.position =0;
					
					if(MusicPlayer.musicInfos.size() <= 0)
					{
						Toast.makeText(MyTabHost.this, "播放列表里没有歌曲。", Toast.LENGTH_LONG).show();
						return;
					}
					
				}
				ServerAccess.activityName="MyTabHost";
				startActivity(intent);
				
				//overridePendingTransition(R.anim.bottom_in,android.R.anim.fade_in);
				overridePendingTransition(R.anim.bottom_in,android.R.anim.fade_out);
			}
			break;
			
			default:
				break;
			}
			
			
		}
	};
	
	private void setTabIndicator(String titleString, int nId, Intent intent)
	{
		// 使用指定样式的TAB
		View view = LayoutInflater.from(this.mTabHost.getContext()).inflate(
				R.layout.tab_style, null);
		String idString = String.valueOf(nId);
		// 创建一个新Tab
		TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(idString)
				.setIndicator(view).setContent(intent);
		mTabHost.addTab(localTabSpec);
	}

	public void createInActivity(int index, Bundle bundle) {
 
	        Intent activityIntent = null;
	        if(index == 0) {
	        	activityIntent = new Intent(this, NativeMusicTab.class);  
	        }else if(index == 1) {
	        	activityIntent = new Intent(this, DownLoadTab.class);
	        }else if(index == 2) {
	        	activityIntent = new Intent(this, MyCollectSingleActivity.class);
	        }else {
	        	
	        }
	        
	        if(bundle != null)
	        	activityIntent.putExtras(bundle);
	        
	        setTabIndicator("", 2, activityIntent);
	        
	        int count = mTabHost.getTabWidget().getChildCount();
			if(count != 3)
				return;
			
	        mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, -1.0f, 0f, 0f, 300, false));
			mTabHost.setCurrentTab(TAB_3);
			mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(1.0f, 0f, 0f, 0f, 300, false));
			
	}
	
	public void returnToActivity(int index) {
		
		int count = mTabHost.getTabWidget().getChildCount();
		if(count != 3)
			return;
		
		mTabHost.clearAllTabs();
		addContent();
		
		mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, 1.0f, 0f, 0f, 300, false));
		mTabHost.setCurrentTab(0);
		mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(-1.0f, 0f, 0f, 0f, 300, false));
	}
	
	private void addContent() {
		mIntent1 = new Intent(this, NativeMusicManageActivity.class);
		mIntent2 = new Intent(this, OnlineTab.class);
		mIntent1.putExtra("mIntent1", true);
		mIntent2.putExtra("mIntent2", true);
		setTabIndicator("", 1, mIntent1);
		setTabIndicator("", 2, mIntent2);
	}
}
